﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.SalePerformance.Record;
using Common.Contract.SalePerformance.Request;
using Common.Contract.SalePerformance.Response;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Core.ServiceCommand
{
    public class RetrieveRankKpiByBranchServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveRankKpiByBranchRequest, RetrieveRankKpiByBranchResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string Branch;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveRankKpiByBranchServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveRankKpiByBranchRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = request.CurrentUser;
            }
            if (string.IsNullOrEmpty(request.Branch))
            {
                throw new ArgumentException("Branch must be set", "request");
            }
            else
            {
                Branch = request.Branch;
            }
        }
        protected override bool OnExecute(RetrieveRankKpiByBranchResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                RankKpiRecord records = new RankKpiRecord();
                TblKpiRankBranch listLoans = new TblKpiRankBranch();
                listLoans = Providers.SalePerformance.RetrieveKpiRankBranchByBranch(Dao, Branch);
                if (listLoans != null)
                {
                    records = new RankKpiRecord
                               {
                                   Yearmonth = listLoans.Yearmonth,
                                   BranchNameSme = listLoans.BranchNameSme,
                                   RankingAccumulated = listLoans.RankingAccumulated,
                                   RankingMonth = listLoans.RankingMonth,
                                   TotalScoreAccumulated = listLoans.TotalScoreAccumulated,
                                   TotalScoreMonth = listLoans.TotalScoreMonth,
                                   CompareLastMonth = listLoans.CompareLastMonth
                               };
                }
                response.Record = records;
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
